package com.deer.wms.base.system.web.task;

import com.deer.wms.base.system.model.bill.BillInMasterCriteria;
import com.deer.wms.base.system.model.bill.BillInMasterDto;
import com.deer.wms.base.system.model.task.Ingot;
import com.deer.wms.base.system.model.task.TaskInfoCriteria;
import com.deer.wms.base.system.model.task.TaskInfoDto;
import com.deer.wms.base.system.service.task.BillInTaskInfoService;
import com.deer.wms.base.system.service.task.ITaskInfoService;
import com.deer.wms.common.core.controller.BaseController;
import com.deer.wms.common.core.domain.AjaxResult;
import com.deer.wms.common.core.page.TableDataInfo;
import com.deer.wms.common.utils.poi.ExcelUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
//晶棒查询
@Controller
@RequestMapping("/task/selectIngot")
public class IngotController extends BaseController {
    String prefix="task";
    @Autowired
    private BillInTaskInfoService billInTaskInfoService;

    @Autowired
    private ITaskInfoService taskInfoService;

    @RequestMapping
    public String view(){
        return prefix+"/ingot";
    }

    @RequestMapping("findList")
    @ResponseBody
    public TableDataInfo list(TaskInfoCriteria taskInfoCriteria){
        startPage();
        TaskInfoCriteria criteria = new TaskInfoCriteria();
        BeanUtils.copyProperties(taskInfoCriteria,criteria);

        String typeParam = taskInfoCriteria.getTypeParam();
        if (null !=typeParam) {
            String[] split = typeParam.split(",");
            String a = split[0];
            String param = split[1];
            int i = Integer.parseInt(a);
            int type = Integer.parseInt(param);

            if (i == 1) {
                taskInfoCriteria.setType(type);
            } else {
                criteria.setType(type);
            }
        }
        List<TaskInfoDto> taskInfoDtos1 = taskInfoService.billOutfindList(taskInfoCriteria);
        List<TaskInfoDto> taskInfoDtos = billInTaskInfoService.billInFindList(criteria);
        taskInfoDtos.addAll(taskInfoDtos1);
        //  List<Ingot> taskInfoCriteria = billInTaskInfoService.selectIngot(ingot);
        return getDataTable(taskInfoDtos);
    }


    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BillInMasterCriteria criteria) {

        List<Ingot> taskInfoCriteria = billInTaskInfoService.selectIngot(null);
        ExcelUtil<Ingot> util = new ExcelUtil<Ingot>(Ingot.class);
        return util.exportExcel(taskInfoCriteria, "晶棒查询");
    }
    /**
     * 导出列表
     */
    //@RequiresPermissions("in:billInMaster:partExcel")
    @PostMapping("/partExcel")
    @ResponseBody
    public AjaxResult partExcel(@RequestParam String ids) {
        List<Ingot> byIds=new ArrayList();
        String[] split = ids.split(",");
        for (String s:split
        ) {
            Ingot b=new Ingot ();
            b.setMaterialId(s);
            List<Ingot> list = billInTaskInfoService.selectIngot(b);
            for (Ingot bs:list
            ) {
                byIds.add(bs);
            }

        }
        // List<BillInSapDto> billInSapList = billInSapService.findBillInSapDtoList(billInSapCriteria);
        ExcelUtil<Ingot> util = new ExcelUtil<Ingot>(Ingot.class);
        return util.exportExcel(byIds, "晶棒查询");
    }
}
